package com.huidu.bitrade.request.util;

import com.alibaba.fastjson2.JSON;
import com.huidu.bitrade.client.exceptions.BinanceClientException;
import com.huidu.bitrade.client.exceptions.BinanceConnectorException;
import com.huidu.bitrade.client.impl.UMFuturesClientImpl;
import com.huidu.bitrade.config.PrivateConfig;
import com.huidu.bitrade.dto.UOrderDTO;
import com.huidu.bitrade.vo.UOrderVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;

@Slf4j
public class BinanceUtil {


    public static UOrderDTO createOrder(UOrderVO orderVO) {
        UOrderDTO uOrderDTO=null;
        LinkedHashMap<String, Object> parameters = JSON.parseObject(JSON.toJSONString(orderVO), LinkedHashMap.class);
        UMFuturesClientImpl client = new UMFuturesClientImpl(PrivateConfig.TESTNET_API_KEY, PrivateConfig.TESTNET_SECRET_KEY, PrivateConfig.TESTNET_BASE_URL);
        try {
            String result = client.account().newOrder(parameters);
            String json = result;
            ObjectMapper mapper = new ObjectMapper();
            try {
                uOrderDTO = mapper.readValue(json, UOrderDTO.class);
                // 使用uOrderDTO对象进行后续操作
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info(result);
        } catch (BinanceConnectorException e) {
            log.error("fullErrMessage: {}", e.getMessage(), e);
        } catch (BinanceClientException e) {
            log.error("fullErrMessage: {} \nerrMessage: {} \nerrCode: {} \nHTTPStatusCode: {}",
                    e.getMessage(), e.getErrMsg(), e.getErrorCode(), e.getHttpStatusCode(), e);
        }
        uOrderDTO.setMId(orderVO.getMId());
        return uOrderDTO;
    }

}
